package com.sunny.shop.api.v1;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sunny.shop.bo.PageCounter;
import com.sunny.shop.exception.httpe.NotFoundException;
import com.sunny.shop.model.Spu;
import com.sunny.shop.service.SupServiceImpl;
import com.sunny.shop.util.CommonUtil;
import com.sunny.shop.vo.PagingDozer;
import com.sunny.shop.vo.SpuSimplifyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.List;

@RestController // RestController 和 Controller 前者包含返回序列化
@Repository("/spu")
@Validated
public class SpuController {
    @Autowired
    public SupServiceImpl supService;
    @GetMapping("id/{id}/detail")
    public Spu getDetail(@PathVariable @Positive Long id){
        Spu detail = supService.getDetail(id);
        if(detail == null){
            throw new NotFoundException(30003);
        }
        return detail;
    }

    // 拷贝测试方法
    @GetMapping("id/{id}")
    public SpuSimplifyVO getOne(@PathVariable @Positive Long id){
        Spu detail = supService.getDetail(id);
        SpuSimplifyVO spuSimplifyVo = new SpuSimplifyVO();
        BeanUtils.copyProperties(detail, spuSimplifyVo); // 使用java的拷贝，实现属性赋值
        return spuSimplifyVo;
    }

    @GetMapping("/by/category/{id}")
    public PagingDozer<Spu,SpuSimplifyVO> getByCategoryId(@RequestParam(name = "start", defaultValue = "0") Integer start,
                                                          @RequestParam(name = "count", defaultValue = "10") Integer count,
                                                          @RequestParam(name = "is_root", defaultValue = "true") Boolean isRoot,
                                                          @PathVariable @Positive Long id){

        PageCounter pageCounter = CommonUtil.converToParameter(start, count);
        Page<Spu> page = supService.getByCategory(id,isRoot,pageCounter.getPage(), pageCounter.getCount());
        return new PagingDozer<>(page, SpuSimplifyVO.class);
    }

    @GetMapping("latest")
    public PagingDozer<Spu, SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
                                                @RequestParam(defaultValue = "10") Integer count){
        // 由于此处接受的参数start为记录的开始值，需转换成page,size形式
        // 如start = 0 count = 2即从第0条开始取两条
        PageCounter pageCounter = CommonUtil.converToParameter(start, count);
        Page<Spu> page = supService.getLatestSpu(pageCounter.getPage(), pageCounter.getCount());
        return new PagingDozer<>(page, SpuSimplifyVO.class);



//        CommonUtil.PageCounter(start,count);
//        Page<Spu> latestSpu = supService.getLatestSpu(start, count);
        /*
        V1.0
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        // 源数据
        List<Spu> latestSpu = supService.getLatestSpu();
        // 创建新数据 使用集合返回
        ArrayList<SpuSimplifyVo> vos = new ArrayList<>();
        latestSpu.forEach(s->{
            SpuSimplifyVo map = mapper.map(s, SpuSimplifyVo.class);
            vos.add(map);
        });
        */
//        return vos;
    }
}
